import 'package:cream_chat_flutter/globals/ThemeProvider.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/HomeTabPageProvider.dart';
import 'package:cream_chat_flutter/pages/UserTabPage/UserTabPageProvider.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/HomeTabPage.dart';
import 'package:cream_chat_flutter/pages/UserTabPage/UserTabPage.dart';

abstract class TabProvider {
  changeTab();
}

///底部导航 这个需要详细设计，这才是真正的大首页
class BottomNavigator extends Page {
  const BottomNavigator({
    LocalKey key,
    String name,
  }) : super(key: key, name: name);

  Route createRoute(BuildContext context) =>
      MaterialPageRoute(
        settings: this,
        builder: (BuildContext context) =>
            ChangeNotifierProvider(
                create: (context) => BottomNavigatorProvider(),
                child: _BottomNavigator()),
      );
}

class _BottomNavigator extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _BottomNavigatorState();

}

class _BottomNavigatorState extends State<_BottomNavigator> {
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  final List<Widget> _pages =  [HomeTabPage(), UserTabPage()];
  final PageController _ctl = PageController(initialPage: 0);

  _bottomItem(String title, IconData icon, int index) =>
      BottomNavigationBarItem(
          icon: Icon(icon),
          activeIcon: Icon(icon),
          label: title);

  @override
  Widget build(BuildContext context) {
    return Consumer2(builder: (BuildContext context, ThemeProvider tp,
        BottomNavigatorProvider bp, Widget child) =>
        Scaffold(
          body: _pages[bp.currentIndex],
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: bp.currentIndex,
            onTap: (index) {
              bp.setCurrentIndex(index);
            },
            type: BottomNavigationBarType.fixed,
            selectedItemColor: _activeColor,
            unselectedItemColor: _defaultColor,
            selectedIconTheme: IconThemeData(color: _activeColor),
            unselectedIconTheme: IconThemeData(color: _defaultColor),
            items: [
              _bottomItem('首页', Icons.home, 0),
              _bottomItem('我的', Icons.account_circle, 1),
            ],
          ),
        )
    );
  }

}

class BottomNavigatorProvider with ChangeNotifier {
  int _currentIndex = 0;

  int get currentIndex => _currentIndex;

  void setCurrentIndex(int idx) {
    _currentIndex = idx;
    notifyListeners();
  }
}

